//
//  NSString+Formatter.m
//  a
//
//  Created by a on 2017/12/19.
//  Copyright © 2017年 a. All rights reserved.
//

#import "NSString+Formatter.h"

@implementation NSString (Formatter)

+ (NSString *)formatterWithNumber:(id)value {
    if (value == nil) {
        return @"";
    }
    return [NSString formatterWithDouble:[value floatValue]];
}

+ (NSString *)formatterWithDouble:(double)value {
    NSString *valueString = [NSString stringWithFormat:@"%.2f", value];
    NSArray *valueArray = [valueString componentsSeparatedByString:@"."];
    NSString *intString = [valueArray safe_objectAtIndex:0];
    NSString *endString = [valueArray safe_objectAtIndex:1];
    if ([intString length] > 4) {
        NSInteger count = [intString length] / 4;
        NSMutableArray *resultArray = [NSMutableArray array];
        for (int i = 0; i < count; i++) {
            NSString *subString = [intString substringWithRange:NSMakeRange([intString length] - 4, 4)];
            intString = [intString substringToIndex:[intString length] - 4];
            [resultArray safe_insertObject:subString atIndex:0];
        }
        if (ValidString(intString)) {
            [resultArray safe_insertObject:intString atIndex:0];
        }
        NSString *resultString = [resultArray componentsJoinedByString:@","];
        return [NSString stringWithFormat:resultString, endString];
    }
    
    return valueString;
}

- (NSString *)numberContainSpace {
    NSString *baseString = [self stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([baseString length] > 4) {
        NSMutableString *resultString = [NSMutableString string];
        for (NSInteger index = 0; index < [baseString length]; index += 4) {
            NSInteger length = MIN([baseString length] - index, 4);
            if (index > 0) {
                [resultString appendString:@" "];
            }
            [resultString appendString:[baseString substringWithRange:NSMakeRange(index, length)]];
        }
        return resultString;
    }
    return baseString;
}

- (NSString *)numberWithoutSpace {
    NSString *number = [self stringByReplacingOccurrencesOfString:@" " withString:@""];
    number = [number stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    number = [number stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    return number;
}

- (NSString *)numberContainStar {
    NSString *spaceString = [self numberContainSpace];
    NSArray *numberArray = [spaceString componentsSeparatedByString:@" "];
    NSMutableString *starString = [NSMutableString string];
    if ([numberArray count] > 1) {
        for (NSInteger i = 0; i < [numberArray count]; i++) {
            if (i == 0) {
                [starString appendString: [numberArray safe_objectAtIndex:i]];
            }
            else if (i == [numberArray count] - 1) {
                [starString appendString:@" "];
                [starString appendString: [numberArray safe_objectAtIndex:i]];
            }
            else {
                [starString appendString:@" ****"];
            }
        }
    }
    else {
        return spaceString;
    }
    return starString;
}

- (BOOL)isValidityPhoneNumber {
    NSString *number = [self stringByReplacingOccurrencesOfString:@" " withString:@""];
    number = [number stringByReplacingOccurrencesOfString:@"\r" withString:@""];
    number = [number stringByReplacingOccurrencesOfString:@"\n" withString:@""];

    NSString *regular = @"1(3|4|5|6|7|8|9)\\d{9}$";
//    NSString *regular = @"^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regular];
    BOOL isMatch = [pred evaluateWithObject:number];
    if (!isMatch) {
        return NO;
    }
    return YES;
}



- (NSString *)errorAboutSixNumberPassword {
    if ([self length] != 6) {
        return @"请输入6位密码";
    }
    NSString *regular = @"^\\d{6}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regular];
    BOOL isMatch = [pred evaluateWithObject:self];
    if (!isMatch) {
        return @"请设置6位数字密码";
    }
    regular = @"(?:(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){5}|(?:9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){5})\\d";
    pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regular];
    isMatch = [pred evaluateWithObject:self];
    if (isMatch) {
        return @"密码过于简单，请重新输入";
    }
    regular = @"([\\d])\\1{5,}";
    pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regular];
    isMatch = [pred evaluateWithObject:self];
    if (isMatch) {
        return @"请不要使用6位重复数字密码";
    }
    
    return nil;
}

- (BOOL)isSixNumberPassword {
    if ([self errorAboutSixNumberPassword] == nil) {
        return YES;
    }
    return NO;
}

- (BOOL)isValidityChineseName {
    NSString *regular = @"[\u4E00-\u9FA5]{2,20}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regular];
    return [pred evaluateWithObject:self];
}

- (BOOL)isValidityFloatNumber {
    NSString *regular = @"^(([1-9]\\d*)|0)(\\.\\d{1,2})?$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regular];
    return [pred evaluateWithObject:self];
}

- (BOOL)isValidityNumber {
    NSString *regular = @"^[0-9]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regular];
    return [pred evaluateWithObject:self];
}

@end
